package exogwt.server.dao;

import exogwt.client.domain.Text;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

/**
 * @author Andriy Zhdanov
 */
public class TextDao extends GenericDao<Text> {
    protected String getNodeType() {
        return "nt:file";
    }

    protected void loadObject(Node node, Text object) {
        String textStr = getContentData(node);
        object.setText(textStr);
    }

    protected void saveObject(Node node, Text object) {
        try {
            Node content = getNode(node, "jcr:content", "nt:resource");
            content.setProperty("jcr:data", object.getText());
        } catch (RepositoryException e) {
            throw new RuntimeException(e);
        }
    }

    private String getContentData(Node node) {
        try {
            Node content = getNode(node, "jcr:content", "nt:resource");
            return getStringProperty(content, "jcr:data", "");
        } catch (RepositoryException e) {
            throw new RuntimeException(e);
        }
    }

    protected Text getInstance() {
        return new Text();
    }
}
